import { defineStore } from 'pinia';

import cookie from '@/utils/cookie';
import { languageTexts } from './lang';
import { navigateTo } from '@/utils/router';

export const useMainStore = defineStore('main', {
  state: () => ({
    websocket: {},
    isMer: 0,
    store: {},
    cart: [],
    orderType: 'takein',
    address: {},
    addresses: {},
    member: {},
    openid: '',
    token: '',
    lang: 'zh-CN',
    rate: 1,
    currencySymbol: '',
    isAtFront: true,
    cookieKey: 'YSESSID=yshop-e4dk4o2utr3c0n95tp42p745ai',
    // 默认地为你为北京地址
    location: {},
    mycoupon: {},
    desk: {},
    isScan: false,
    isManualSetLang: false,
    uidType: 'user',
    merchartShop: {}, //商家店铺
    currentLanguage: {
      key: 'zh-CN',
      label: '中文',
    },
    tgUser: {
      chat_id: '',
      user_name: '',
    },
    isNormalReturn: false,
    languageTypes: [],
  }),
  getters: {
    isLogin(state) {
      //是否登录
      return Object.keys(state.member).length > 0;
      //return cookie.get('accessToken') ? true : false
    },
    currentLanguageTexts: (state) => languageTexts[state.currentLanguage.key],
    //isLogin: state => Object.keys(state.member).length > 0	//是否登录
  },
  persist: {
    enabled: true, // 启用持久化
    strategies: [
      {
        storage: {
          setItem(key, value) {
            uni.setStorageSync(key, value);
          },
          getItem(key) {
            return uni.getStorageSync(key);
          },
        },
        paths: ['member', 'currentLanguage', 'isManualSetLang'], // 持久化这个字段
      },
    ],
  },
  actions: {
    SET_WEB_SOCKET(val) {
      this.websocket = val;
    },
    SET_UID_TYPE(val) {
      this.uidType = val;
    },
    SET_MER(val) {
      this.isMer = val;
    },
    SET_MERCHART_SHOP(shop) {
      this.merchartShop = shop;
    },
    SET_DESK(desk) {
      this.desk = desk;
      this.isScan = true;
    },
    DEL_DESK() {
      this.desk = {};
      this.isScan = false;
    },
    SET_COUPON(coupon) {
      this.mycoupon = coupon;
    },
    DEL_COUPON() {
      this.mycoupon = {};
    },
    SET_ORDER_TYPE(type) {
      this.orderType = type;
    },
    SET_MEMBER(member) {
      this.member = member;
      cookie.set('userinfo', member);
    },
    SET_ADDRESS(address) {
      this.address = address;
    },
    SET_ADDRESSES(addresses) {
      this.addresses = addresses;
    },
    SET_STORE(store) {
      console.log('store111111', store);
      this.store = store;
    },
    SET_CART(cart) {
      this.cart = cart;
    },
    REMOVE_CART(state) {
      this.cart = [];
    },
    setCookie(state, provider) {
      state.cookie = provider;
      uni.setStorage({
        key: 'cookieKey',
        data: provider,
      });
    },
    SET_LOCATION(location) {
      this.location = location;
    },
    SET_OPENID(openid) {
      this.openid = openid;
    },
    SET_TOKEN(token) {
      this.token = token;
      cookie.set('accessToken', token);
    },

    setAccessToken(user) {
      cookie.set('accessToken', user);
      // return getUserInfo()
    },
    setSelectAddress(id) {
      console.log('--> % setSelectAddress % id:\n', id);
      this.selectAddress = this.address.filter((item) => item.id == id)[0];
    },
    init() {
      let accessToken = cookie.get('accessToken');
      if (accessToken) {
        //return getUserInfo()
      }
      return null;
    },
    logout() {
      this.user = null;
      this.address = [];
      this.areaList = [];
      this.selectAddress = null;
      navigateTo('/pages/components/pages/login/login');
    },
    SET_RETURN(val) {
      this.isNormalReturn = val;
    },
  },
});
